import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/components/Login.vue';
import Register from '@/components/Register.vue';
import Helloworld from '@/components/HelloWorld.vue'
import clearUser from '@/components/clearUsers.vue'

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
  },
  {
    path: '/HelloWorld',
    name: 'HelloWorld',
    component: Helloworld,
  },
  {
    path: '/clearUsers',
    name: 'ClearUsers',
    component: clearUser,
  },
];

const router = new VueRouter({
  mode: 'hash',
  routes,
});

router.beforeEach((to, from, next) => {
  // 如果用户访问的是根路径，则重定向到登录页面
  if (to.path === '/') {
    return next('/login');
  }
  next();
});

export default router;
